package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.UserFeedback;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface UserFeedbackMapper {
    // 获取所有用户反馈列表（包含所有字段）
    @Select("SELECT feedback_id, user_id, feedback_type, title, content, status, admin_reply, created_at, updated_at " +
            "FROM user_feedbacks ORDER BY created_at DESC")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "feedbackType", column = "feedback_type"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "status", column = "status"),
            @Result(property = "adminReply", column = "admin_reply"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    List<UserFeedback> getAllFeedbacks();

    // 根据条件获取反馈列表（分页+筛选）
    @Select("<script>" +
            "SELECT f.feedback_id, f.user_id, u.username, f.feedback_type, f.title, f.content, f.status, f.admin_reply, f.created_at, f.updated_at " +
            "FROM user_feedbacks f LEFT JOIN users u ON f.user_id = u.user_id " +
            "WHERE 1=1 " +
            "<if test='timeCondition != null'>${timeCondition}</if> " +
            "<if test='status != null'>AND f.status = #{status}</if> " +
            "<if test='feedbackType != null'>AND f.feedback_type = #{feedbackType}</if> " +
            "<if test='searchCondition != null'>${searchCondition}</if> " +
            "ORDER BY f.created_at DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "feedbackType", column = "feedback_type"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "status", column = "status"),
            @Result(property = "adminReply", column = "admin_reply"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    List<UserFeedback> getFeedbackListByCondition(Map<String, Object> params);

    // 根据条件获取反馈总数
    @Select("<script>" +
            "SELECT COUNT(*) FROM user_feedbacks f " +
            "LEFT JOIN users u ON f.user_id = u.user_id " +
            "WHERE 1=1 " +
            "<if test='timeCondition != null'>${timeCondition}</if> " +
            "<if test='status != null'>AND f.status = #{status}</if> " +
            "<if test='feedbackType != null'>AND f.feedback_type = #{feedbackType}</if> " +
            "<if test='searchCondition != null'>${searchCondition}</if> " +
            "</script>")
    int getFeedbackCountByCondition(Map<String, Object> params);

    // 获取反馈总数
    @Select("SELECT COUNT(*) FROM user_feedbacks")
    int getFeedbackCount();

    // 根据ID获取反馈详情
    @Select("SELECT f.feedback_id, f.user_id, u.username, f.feedback_type, f.title, f.content, f.status, f.admin_reply, f.created_at, f.updated_at " +
            "FROM user_feedbacks f LEFT JOIN users u ON f.user_id = u.user_id " +
            "WHERE f.feedback_id = #{id}")
    @Results({
            @Result(property = "feedbackId", column = "feedback_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "feedbackType", column = "feedback_type"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "status", column = "status"),
            @Result(property = "adminReply", column = "admin_reply"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    UserFeedback getFeedbackById(String id);

    // 根据ID删除用户反馈
    @Delete("DELETE FROM user_feedbacks WHERE feedback_id = #{id}")
    int deleteFeedbackById(String id);

    // 添加用户反馈（可选）
    @Insert("INSERT INTO user_feedbacks(feedback_id, user_id, feedback_type, title, content, status, admin_reply, created_at, updated_at) " +
            "VALUES(#{feedbackId}, #{userId}, #{feedbackType}, #{title}, #{content}, #{status}, #{adminReply}, #{createdAt}, #{updatedAt})")
    int addFeedback(UserFeedback feedback);
}